#!/bin/bash

# This is a source module.

base64.encode () 
{ 
    ( declare -i limit=0 setClip=0 fSize=0 mode=0 width=76;
    function this.err () 
    { 
        echo "${FUNCNAME[1]}: ""$@" 1>&2;
        b64k;
        echo 'return 1'
    };
    declare b64a='base64 -w $width > /tmp/b64.tmp';
    function b64t () 
    { 
        eval $b64a
    };
    function b64s () 
    { 
        local lsfs;
        lsfs=($(env ls -l /tmp/b64.tmp 2>&-));
        echo ${lsfs[4]}
    };
    function b64o () 
    { 
        echo "$(< /tmp/b64.tmp)";
        rm /tmp/b64.tmp
    };
    function b64c () 
    { 
        cat /tmp/b64.tmp | xsel -i -b;
        rm /tmp/b64.tmp
    };
    function b64v () 
    { 
        echo -n "${!1}" | b64t
    };
    function b64k () 
    { 
        [[ -e /tmp/b64.tmp ]] && rm /tmp/b64.tmp
    };
    function know.command () 
    { 
        builtin type "$1" 2>&- > /dev/null
    };
    know.command base64 || { 
        $(this.err "cannot decode data please install GNU coreutils")
    };
    while [[ "${1:0:1}" = "-" ]]; do
        [[ "${1:1:1}" =~ [fvg] ]] && [[ $mode -gt 0 ]] && { 
            $(this.err "invalid operand, mode already specified")
        };
        [[ "$1" = "--" ]] && { 
            [[ $mode -gt 0 ]] && { 
                shift;
                break
            };
            mode=1;
            shift;
            break
        };
        [[ "${1:0:3}" = "-if" ]] && { 
            declare varF;
            [[ "${1:3:1}" =~ [':='] ]] && { 
                varF=${1:4};
                shift
            } || { 
                [[ $# -gt 1 ]] && { 
                    shift;
                    varF=$1;
                    shift
                } || { 
                    $(this.err "option -if, not enough arguments")
                }
            };
            [[ -z "$varF" ]] && { 
                $(this.err "option -if, file is null")
            };
            mode=2;
            continue
        };
        [[ "$1" = "-iv" ]] && { 
            declare varN;
            [[ "${1:3:1}" =~ [':='] ]] && { 
                varN=${1:4};
                shift
            } || { 
                [[ $# -gt 1 ]] && { 
                    shift;
                    varN=$1;
                    shift
                } || { 
                    $(this.err "option -iv, not enough arguments")
                }
            };
            [[ -z "$varN" ]] && { 
                $(this.err "option -iv, label is null")
            };
            mode=3;
            continue
        };
        [[ "$1" = "-oc" ]] && { 
            know.command xsel && { 
                setClip=1;
                shift;
                continue
            } || { 
                $(this.err "cannot write clipboard please install xsel")
            }
        };
        [[ "$1" = "-ic" ]] && { 
            know.command xsel || { 
                $(this.err "cannot read clipboard please install xsel")
            };
            mode=4;
            shift;
            continue
        };
        [[ "$1" = "-e" ]] && { 
            [[ -n "$z" ]] && { 
                $(this.err "invalid switch -e, compression already defined as $z")
            };
            declare z=encrypted;
            shift;
            b64a='zip -e -q -fz- | base64 -w $width > /tmp/b64.tmp';
            continue
        };
        [[ "$1" = "-z" ]] && { 
            [[ -n "$z" ]] && { 
                $(this.err "invalid switch -z, compression already defined as $z")
            };
            declare z=enabled;
            shift;
            b64a='zip -q -fz- | base64 -w $width > /tmp/b64.tmp';
            continue
        };
        [[ "${1:0:2}" = "-s" ]] && { 
            [[ "${1:2:1}" =~ [':='] ]] && { 
                limit=${1:3};
                shift
            } || { 
                shift;
                limit=$1;
                shift
            };
            [[ -n "$limit" ]] && { 
                [[ "$(printf '%d' $width 2>&1)" -eq "$width" ]] 2>&- || { 
                    $(this.err "invalid number specified for limit")
                };
                [[ $limit -gt 0 ]] || { 
                    $(this.err "negative number specified for limit")
                }
            };
            continue
        };
        [[ "${1:0:2}" = "-w" ]] && { 
            [[ "${1:2:1}" =~ [':='] ]] && { 
                width=${1:3};
                shift
            } || { 
                shift;
                width=$1;
                shift
            };
            [[ -n "$width" ]] && { 
                [[ "$(printf '%d' $width 2>&1)" -eq "$width" ]] 2>&- || { 
                    $(this.err "invalid number specified for width")
                };
                [[ $width -gt 0 ]] || { 
                    $(this.err "negative number specified for width")
                }
            };
            continue
        };
        [[ $mode -eq 0 ]] && { 
            break
        } || { 
            $(this.err "unknown option: \"$1\"")
        };
    done;
    [[ $mode -eq 0 ]] && { 
        echo -n "$@" | b64t;
        fSize=$(b64s);
        [[ $fSize -eq 0 ]] && { 
            $(this.err "immediate data is null")
        };
        [[ $limit -gt 0 ]] && [[ $fSize -gt $(( $limit - 1 )) ]] && { 
            $(this.err "immediate data out of bounds")
        };
        [[ $setClip -eq 1 ]] && { 
            b64c
        } || { 
            b64o
        };
        return
    };
    [[ $mode -eq 1 ]] && { 
        b64t;
        fSize=$(b64s);
        [[ $fSize -eq 0 ]] && { 
            $(this.err "STDIN data is null")
        };
        [[ $limit -gt 0 ]] && [[ $fSize -gt $(( $limit - 1 )) ]] && { 
            $(this.err "input out of bounds")
        };
        [[ $setClip -gt 0 ]] && { 
            b64c;
            return
        };
        b64o;
        return
    };
    [[ $mode -eq 2 ]] && { 
        [[ -e "$varF" ]] && { 
            [[ -r "$varF" ]] || { 
                $(this.err "cannot read file \"$varF\"")
            };
            cat "$varF" | b64t;
            fSize=$(b64s);
            [[ $fSize -eq 0 ]] && { 
                $(this.err "input file is empty")
            };
            [[ $limit -gt 0 ]] && [[ $fSize -gt $(( $limit - 1 )) ]] && { 
                $(this.err "input file out of bounds")
            };
            [[ $setClip -eq 1 ]] && { 
                b64c;
                return
            };
            b64o;
            return
        } || { 
            $(this.err "file \"$varF\" does not exist")
        }
    };
    [[ $mode -eq 3 ]] && { 
        b64v $varN;
        fSize=$(b64s);
        [[ $fSize -eq 0 ]] && { 
            $(this.err "variant data is null")
        };
        [[ $limit -gt 0 ]] && [[ $fSize -gt $(( $limit - 1 )) ]] && { 
            $(this.err "input data out of bounds")
        };
        [[ $setClip -eq 1 ]] && { 
            b64c;
            return
        };
        b64o;
        return
    };
    [[ $mode -eq 4 ]] && { 
        xsel -o -b | b64t;
        fSize=$(b64s);
        [[ $fSize -eq 0 ]] && { 
            $(this.err "clipboard data is null")
        };
        [[ $limit -gt 0 ]] && [[ $fSize -gt $(( $limit - 1 )) ]] && { 
            $(this.err "input data out of bounds")
        };
        [[ $setClip -eq 1 ]] && { 
            b64c;
            return
        };
        b64o;
        return
    } )
}
base64.decode () 
{ 
    ( declare -i iMode=0 oMode=0;
    declare tmp='/tmp/b64.dat';
    declare b64d="cat $tmp | base64 -d" odc='';
    function this.err () 
    { 
        echo "${FUNCNAME[1]}: ""$@" 1>&2;
        rm -f $tmp;
        echo 'return 1'
    };
    function file.accessible () 
    { 
        [[ -e "$1" ]] && [[ -r "$1" ]]
    };
    function file.empty () 
    { 
        local lsfs;
        lsfs=($(env ls -l "$1" 2>&-));
        [[ "${lsfs[4]}" = "0" ]] || [[ -z "${lsfs[4]}" ]]
    };
    function modeLocked () 
    { 
        [[ $1 -gt 0 ]] && { 
            echo "${FUNCNAME[1]}: $2 mode has already been set" 1>&2;
            rm -f $tmp;
            echo 'return 1'
        }
    };
    function qParam () 
    { 
        [[ "${1:2:1}" =~ [':='] ]] && { 
            eval $3="${1:3}";
            return 1
        } || { 
            eval $3="$2";
            return 2
        }
    };
    local getParam='eval qParam "$1" "$2"';
    function know.command () 
    { 
        builtin type "$1" 2>&- > /dev/null
    };
    know.command base64 || { 
        $(this.err "cannot decode data please install GNU coreutils")
    };
    while [[ "${1:0:1}" = '-' ]]; do
        [[ "$1" = '--' ]] && { 
            $(modeLocked $iMode input);
            shift;
            iMode=1;
            continue
        };
        [[ "${1:0:3}" = '-if' ]] && { 
            $(modeLocked $iMode input);
            local iFile='';
            $getParam iFile;
            [[ -z "$iFile" ]] && $(this.err "input file is null");
            file.accessible "$iFile" && { 
                ! file.empty "$iFile" && { 
                    cp "$iFile" "$tmp"
                } || { 
                    $(this.err "input file \"$iFile\" is empty.")
                }
            } || { 
                $(this.err "file \"$iFile\" is not accessible")
            };
            shift $?;
            iMode=2;
            continue
        };
        [[ "${1:0:3}" = '-of' ]] && { 
            $(modeLocked $oMode output);
            local oFile='';
            $getParam oFile;
            [[ -z "$oFile" ]] && $(this.err "option -of, output file is null");
            odc=" >\"$oFile\"";
            shift $?;
            oMode=1;
            continue
        };
        [[ "${1:0:3}" = '-iv' ]] && { 
            $(modeLocked $iMode input);
            local iVar='';
            $getParam iVar;
            [[ -z "$iVar" ]] && $(this.err "option -iv, input label is null");
            echo -n "${!iVar}" > "$tmp";
            file.empty "$tmp" && { 
                $(this.err "option -iv, input data is null")
            };
            shift $?;
            iMode=3;
            continue
        };
        [[ "$1" = '-ic' ]] && { 
            know.command xsel || { 
                $(this.err "cannot read clipboard please install xsel")
            };
            $(modeLocked $iMode input);
            xsel -o -b > "$tmp";
            file.empty "$tmp" && { 
                $(this.err "option -ic, clipboard data is empty")
            };
            shift;
            iMode=4;
            continue
        };
        [[ "$1" = '-oc' ]] && { 
            know.command xsel || { 
                $(this.err "cannot write clipboard please install xsel")
            };
            $(modeLocked $oMode output);
            odc=' | xsel -i -b';
            shift;
            oMode=1;
            continue
        };
        [[ $iMode -eq 0 ]] && { 
            break
        } || { 
            $(this.err "unknown option $1")
        };
    done;
    [[ $iMode -eq 0 ]] && { 
        echo -n "$@" > "$tmp";
        file.empty "$tmp" && { 
            $(this.err "input is null")
        }
    };
    [[ $(head -c 9 "$tmp") = "UEsDBBQAC" ]] && { 
        know.command funzip || { 
            $(this.err "cannot decompress data please install funzip")
        };
        b64d+=" | funzip"
    };
    [[ $oMode -gt 0 ]] && b64d+=" $odc";
    eval "$b64d" )
}

